package org.example.mybatis.Dao;

import org.apache.ibatis.annotations.*;
import org.example.mybatis.model.UserInfo;

import java.util.List;

@Mapper
public interface UserInfoMapper {

    @ResultMap("baseMap")
    @Select("select * from userinfo")
    public List<UserInfo> queryAllUser();

    @Select("select username, `password`, age, gender, phone from userinfo where id= #{id}")
    public UserInfo queryById(Integer id);

    @Select("select * from userinfo where id = #{id1} and age = #{age1}")
    public List<UserInfo> query(@Param("id1") Integer id, @Param("age1") Integer age);

    //    @Insert("insert into userinfo (username, `password`, age, gender, phone) " +
//            "values(#{username}, #{password}), #{age}, #{gender}, #{phone})")
    //@ResultMap("baseMap")

    /**
     * @options 获取自增id
     * @param userInfo
     * @return
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo (username, `password`, age, gender, phone) " +
            "values(#{username},#{password},#{age},#{gend},#{phone})")

    public Integer insert1(UserInfo userInfo);

    @Insert("insert into userinfo (username, `password`, age, gender, phone) " +
            "values(#{user.username}, #{user.password}, #{user.age}, #{user.gender}, #{user.phone})")
    public Integer insert2(@Param("user") UserInfo userInfo);

    @Results(id = "baseMap", value = {
            @Result(column = "gender", property = "gend"),
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    @Select("select gender as gend from userinfo")
    public List<UserInfo> queryAll1();

    @Update("update userinfo set username = 'zhaoliu' where id = #{id}")
    public Integer updateById(Integer id);

    @Delete("delete from userinfo where username = #{username}")
    public Integer deleteByUsername(String username);
}
